package com.qqtech.qquan.sms.service;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.JsonUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.sms.cache.SmsSysParamCache;
import com.qqtech.qquan.sms.dao.SmsBaseDao;
import com.qqtech.qquan.sms.enums.SmsConst;
import com.qqtech.qquan.sms.model.SmsBase;
import com.qqtech.qquan.sms.model.SmsProvider;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 阿里大鱼短信平台接口实现
 * 
 * @author andy.wangzhh
 *
 */
@Service("smsApiAliService")
public class SmsApiAliServiceImpl extends BaseServiceImpl<SmsBase>implements SmsApiService {
	public static final Logger log = LoggerFactory.getLogger(SmsApiAliServiceImpl.class);

	private SmsProvider smsProvider;
	@Resource
	private SmsProviderService smsProviderService;
	@Resource
	private SmsSysParamCache smsSysParamCache;
	@Resource
	private SmsBaseDao smsBaseDao;

	@Override
	protected BaseDao<SmsBase> getBaseDao() {
		return smsBaseDao;
	}

	public SmsProvider getSmsProvider() {
		return smsProvider;
	}

	public void setSmsProvider(SmsProvider smsProvider) {
		this.smsProvider = smsProvider;
	}

	@PostConstruct
	public void init() {
		this.smsProvider = smsProviderService
				.getById(smsSysParamCache.getInt(SmsConst.PARAMKEY_SMS_PROVIDER_ID_ALIDAYU));
	}

	@Override
	public Result doSend(String mobile, String spTemplateId, Map<String, String> params) {
		if (smsProvider == null) {
			return new Result(RespEnum.EXCEPTION, "短信厂商(阿里大鱼)不存在");
			// throw new SmsException("短信厂商(阿里大鱼)不存在");
		}
		Result result = new Result();
		if (StringUtil.isBlank(mobile)) {
			result.addErr("接收手机号码不能为空");
		} else if (mobile.indexOf(";") >= 0) {
			if (smsProvider.getIsMass() == null || YesNoEnum.YES.getCode() != smsProvider.getIsMass().intValue()) {
				result.addErr("厂商" + smsProvider.getName() + "不支持短信群发功能");
			} else {
				mobile = mobile.replaceAll(";", ",");
			}
		}
		if (StringUtil.isBlank(spTemplateId)) {
			result.addErr("模板参数不能为空");
		}
		if (!result.checkForm()) {
			result.setCode(RespEnum.PARAMERROR.getCode());
			return result;
		}
		TaobaoClient client = new DefaultTaobaoClient(smsProvider.getAppUrl(), smsProvider.getAppKey(),
				smsProvider.getAppSecret());
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		// req.setExtend("7718");
		req.setSmsType("normal");
		req.setSmsFreeSignName(smsSysParamCache.get(SmsConst.PARAMKEY_SMS_DEFAULT_SIGNATURE));
		req.setSmsParamString(JsonUtil.toJson(params));
		req.setRecNum(mobile);
		req.setSmsTemplateCode(spTemplateId);
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (rsp == null || !rsp.isSuccess()) {
				result.setCode(RespEnum.EXCEPTION.getCode());
				result.setMsg("短信发送异常");
				if (rsp != null) {
					result.setMsg(rsp.getMsg());
				}
			} else {
				result.setCode(RespEnum.OK.getCode());
				result.setMsg(rsp.getMsg());
			}
		} catch (ApiException e) {
			log.error("调用短信发送接口发送失败:服务商-{},服务商模板-{},异常信息-{}", smsProvider.getName(), spTemplateId, e);
			e.printStackTrace();
			result.setCode(RespEnum.EXCEPTION.getCode());
			result.setMsg(RespEnum.EXCEPTION.getDesc());
			return result;
		}
		return result;
	}
}